package com.hstx.demo.mqtt.controller.xml;

import com.hstx.demo.mqtt.UiTest;
import com.hstx.demo.mqtt.controller.MqttController;
import com.hstx.demo.mqtt.controller.MqttRecivePushCallBack;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.net.URL;
import java.util.ResourceBundle;

@Controller
public class UiMainController implements Initializable {

    public TextField txtMqttServerAdd;
    public TextField txtMqttClientId;
    public TextField txtUserName;
    public TextField txtPassword;
    public Button btnConnectOrDisConnect;
    public TextField txtTTopic;
    public Button btnSubscribe;
    public TextField txtPublicTopic;
    public TextArea txtareMsg;
    public TextField txtQOS;
    public Button btnPuslish;
    public Button btnUnSubscribe;
    public TextArea txtareRecevieMsg;
    public Button btnTest001;

    private boolean isConnect = false;
    @Autowired
    private MqttController mqttController;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        mqttController.setCallBack(new MqttRecivePushCallBack(txtareRecevieMsg));

        btnConnectOrDisConnect.setOnMouseClicked(event -> {
            mqttController.setHostUrl(txtMqttServerAdd.getText());
            mqttController.setClientId(txtMqttClientId.getText());
            mqttController.setUsername(txtUserName.getText());
            mqttController.setPassword(txtPassword.getText());
            if (!isConnect) {
                mqttController.connect();
                isConnect = true;
                btnConnectOrDisConnect.setText("断开链接");
//                btnUnSubscribe.setDisable(true);
                txtareRecevieMsg.appendText("### 已链接到MQTT服务器" + "\n\n");
            } else {
                mqttController.disConnect();
                isConnect = false;
                btnConnectOrDisConnect.setText("链接MQTT");
//                btnUnSubscribe.setDisable(false);
                txtareRecevieMsg.appendText("\n\n" + "### 已从MQTT服务器断开链接");
            }
        });

        btnSubscribe.setOnMouseClicked(event -> {
            mqttController.setSubscribeTopic(txtTTopic.getText());
            mqttController.setSubscribeQos(Integer.valueOf(txtQOS.getText()));
            mqttController.subTopic();
            txtareRecevieMsg.appendText(">>> 已成功订阅" + txtTTopic.getText() + "\n");
        });

        btnUnSubscribe.setOnMouseClicked(event -> {
            mqttController.setSubscribeTopic(txtTTopic.getText());
            mqttController.setSubscribeQos(Integer.valueOf(txtQOS.getText()));
            mqttController.cleanTopic();
        });

        btnPuslish.setOnMouseClicked(event -> {
            mqttController.publishMeaasge(txtPublicTopic.getText().trim(), txtareMsg.getText().trim(), Integer.parseInt(txtQOS.getText()));
        });

        btnTest001.setOnMouseClicked(event -> {
            UiTest uiTest = new UiTest();
            uiTest.start((Stage) btnTest001.getScene().getWindow());
        });
    }
}
